package dao

import (
	"context"
	"fmt"
	"gorm.io/gorm"
)

type SafetyEducationDAO struct {
	db *gorm.DB
}

func NewSafetyEducationDap(db *gorm.DB) *SafetyEducationDAO {
	return &SafetyEducationDAO{
		db: db,
	}
}

func (dao *SafetyEducationDAO) Query(ctx context.Context, s SafetyEducation) ([]SafetyEducation, error) {
	var safetyEducations []SafetyEducation
	db := dao.db

	if category := s.Category; category != "" {
		db = db.WithContext(ctx).Where("category = ?", category)
	}
	if moduleName := s.ModuleName; moduleName != "" {
		moduleName = fmt.Sprintf("%%%v%%", moduleName)
		db = db.WithContext(ctx).Where("name like ?", moduleName)
	}

	//if s.Category == "" && s.ModuleName == "" {
	//	return []SafetyEducation{}, nil
	//}
	err := db.Table("safety_learning_module").Debug().WithContext(ctx).Find(&safetyEducations).Error
	if err != nil {
		fmt.Println("query error")
	}
	return safetyEducations, nil
}

type SafetyEducation struct {
	Id           int `gorm:"autoIncrement, primary_key"`
	Category     string
	ModuleName   string `gorm:"column:name"`
	ModuleNameEn string `gorm:"column:name_en"`
	Content      string `gorm:"column:content"`
	ContentEn    string `gorm:"column:content_en"`
}
